//
//  CJTemplateRectHelper.m
//  TSDemo_ImageFilter
//
//  Created by ciyouzen on 2014/5/8.
//  Copyright (c) 2014年 dvlproad. All rights reserved.
//

#import "CJTemplateRectHelper.h"
#import <UIPathCJHelper/UIBezierPathCJHelper.h>

@implementation CJTemplateRectHelper

/*
 *  根据【完整模板的大小】获取【子模板上的所有点】等比缩放/映射到实际要显示的父frame后，得到【新point、在父frame中包含新point的frame、新点构成的path】
 *
 *  @param subTemplateRectPoints    子模板上的所有点
 *  @param fullTemplateSize         完整模板的大小
 *  @param realSuperFrame           最后得到的子frame所在的视图区域
 *
 *  @return 【子模板上的所有点】在实际要显示的父frame上的各种信息（新point、在父frame中包含新point的frame、新点构成的path）
 */
+ (CJTemplatePositionModel *)realTemplateModelWithTemplatePointStrings:(NSArray<NSString *> *)subTemplateRectPoints
                                              fullTemplateSize:(CGSize)fullTemplateSize
                                           mapToRealSuperFrame:(CGRect)realSuperFrame
{
    CJTemplatePositionModel *realTemplateModel = [[CJTemplatePositionModel alloc] init];
    // 1、根据【完整模板的大小】将其上的【子模板的点】等比缩放/映射到实际要显示的父frame，得到最后的模板子点
    NSArray<NSString *> *realTemplatePointStrings = [self realTemplatePointStringsForTemplatePointArray:subTemplateRectPoints
                                                                                       fullTemplateSize:fullTemplateSize
                                                                                    mapToRealSuperFrame:realSuperFrame];
    realTemplateModel.realTemplatePointStrings = realTemplatePointStrings;
    
    // 2、找到刚好包含着【子模板上点】的区域
    CGRect realSubFrame = [self rectWhichContainPointStrings:realTemplatePointStrings];
    realTemplateModel.realSubFrame = realSubFrame;
    
    // 3、获取由【子模板上点】构成的规则或不规则的路径
    UIBezierPath *realTemplatePointPath = [UIBezierPathCJHelper bezierPathForPointStrings:realTemplatePointStrings
                                                                               inHisFrame:realSubFrame];
    realTemplateModel.realTemplatePointPath = realTemplatePointPath;
    
    return realTemplateModel;
}


/*
 *  获取实际点：根据【完整模板的大小】将其上的【子模板的点】等比缩放/映射到实际要显示的父frame，得到最后的模板子点
 *
 *  @param subTemplateRectPoints    子模板上的所有点
 *  @param fullTemplateSize         完整模板的大小
 *  @param realSuperFrame           最后得到的子模板所在的视图区域
 *
 *  @return 实际的子模板的点（已映射到realSuperFrame）
 */
+ (NSArray<NSString *> *)realTemplatePointStringsForTemplatePointArray:(NSArray<NSString *> *)subTemplateRectPoints
                                                      fullTemplateSize:(CGSize)fullTemplateSize
                                                   mapToRealSuperFrame:(CGRect)realSuperFrame
{
    NSMutableArray<NSString *> *realTemplatePointStrings = [[NSMutableArray alloc] init];
    for (NSString *subTemplatePointString in subTemplateRectPoints) {
        CGPoint subTemplatePoint = CGPointFromString(subTemplatePointString);
        
        CGFloat realTemplatePointX = subTemplatePoint.x * realSuperFrame.size.width/fullTemplateSize.width * 1.0;
        realTemplatePointX = ceil(realTemplatePointX);  // 向上取整，修复计算误差导致的，可能出现模块连接间有缝隙/或者说通过缝隙看到了底部的背景色
        CGFloat realTemplatePointY = subTemplatePoint.y * realSuperFrame.size.height/fullTemplateSize.height * 1.0;
        realTemplatePointY = ceil(realTemplatePointY);  // 向上取整，修复计算误差导致的，可能出现模块连接间有缝隙/或者说通过缝隙看到了底部的背景色
        
        CGPoint realTemplatePoint = CGPointMake(realTemplatePointX, realTemplatePointY);
        //NSLog(@"模板Cell上个各点在真正坐标系下的位置:%@", NSStringFromCGPoint(realTemplatePoint));

        NSString *realTemplatePointString = NSStringFromCGPoint(realTemplatePoint);
        [realTemplatePointStrings addObject:realTemplatePointString];
    }
    
    return realTemplatePointStrings;
}


/*
 *  获取能刚好包含住指定的所有点的最小rect
 *
 *  @param pointStrings     rect要包含住的点
 *
 *  @return 刚好包含住指定的所有点的最小rect
 */
+ (CGRect)rectWhichContainPointStrings:(NSArray<NSString *> *)pointStrings {
    // 1、从points遍历找到上下左右四个点
    CGFloat minX = INT_MAX;
    CGFloat maxX = 0;
    CGFloat minY = INT_MAX;
    CGFloat maxY = 0;
    for (int i = 0; i < [pointStrings count]; i++) {
        NSString *templatePointString = [pointStrings objectAtIndex:i];
        CGPoint templatePoint = CGPointFromString(templatePointString);
        if (templatePoint.x <= minX) {
            minX = templatePoint.x;
        }
        if (templatePoint.x >= maxX) {
            maxX = templatePoint.x;
        }
        if (templatePoint.y <= minY) {
            minY = templatePoint.y;
        }
        if (templatePoint.y >= maxY) {
            maxY = templatePoint.y;
        }
    }
    
    
    
    CGFloat rectWidth = maxX - minX;
    CGFloat rectHeight = maxY - minY;
    CGRect rect = CGRectMake(minX, minY, rectWidth, rectHeight);
    //NSLog(@"模板所占的矩形框区域:%@", NSStringFromCGRect(rect));
    
    return rect;
}

@end
